package cn.wizzer.app.user.modules.service.impl;

import cn.wizzer.app.user.modules.models.Goods;
import cn.wizzer.app.user.modules.services.GoodsService;
import cn.wizzer.framework.base.service.BaseServiceImpl;
import cn.wizzer.framework.page.Pagination;
import com.alibaba.dubbo.config.annotation.Service;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;

@IocBean(args = {"refer:dao"})
@Service(interfaceClass = GoodsService.class)
public class GoodsServiceImpl extends BaseServiceImpl<Goods> implements GoodsService {
    public GoodsServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listPage(Integer pageNumber, int pageSize, String searchKeyword, String id, String goodsCategory) {
        Sql sql = Sqls.create("select * from goods $condition");
        Sql countSql = Sqls.create("select count(1) from goods $condition");
        Cnd cnd = Cnd.where("delFlag", "=", 0)
                .and("disabled", "=", 0);
        if (Strings.isNotBlank(goodsCategory)) {
            cnd.and("goods_category", "=", goodsCategory);
        } else if (Strings.isNotBlank(id)) {
            cnd.and("id", "=", id);
        }
        if(Strings.isNotBlank(searchKeyword)){
            searchKeyword = "%" + searchKeyword + "%";
            cnd.and("name", "like", searchKeyword);
        }
        sql.setCondition(cnd);
        sql.appendSourceSql("order by opAt desc");
        countSql.setCondition(cnd);
        return this.listPage(pageNumber, pageSize, sql, countSql);
    }

}
